﻿Public Class ClinitekStatus
    'Hàm kiểm tra xem xét nghiệm thực hiện có thành công hay không?
    Public Function Check(ByVal input As String, ByRef result As String) As Boolean
        Dim err() As String = {"E01", "E02", "E03", "E04", "E10", "E11", "E12", "E20", "E23", "E24", "E25", "E27", "E28", "E48", "E50", "E52", "E53", "E54", "E55", "E56", "E57", "E58", "E59", "E60", "E61", "E62", "E63", "E64", "E65", "E66", "E67", "E68"}
        Dim flag As Integer = 0

        For Each str As String In err
            If (input.IndexOf(str) <> -1) Then
                result = str
                flag = 1
                Return False

            End If
        Next
        If (flag = 0) Then
            result = ""
            Return True
        End If

    End Function
    'Hàm trả về các thông số chung của một xét nghiệm
    Public Sub resultcommon(ByVal input As String, ByRef SerialInstrument As Integer, ByRef PatientName As String, ByRef PatientID As Integer, ByRef TestProtocol As String, ByRef TestDate As String, ByRef TestTime As String, ByRef OperatorID As Integer, ByRef TestNumber As Integer, ByRef Color As String, ByRef Clarity As String)
        Dim temp_testnumber As String = ""
        Dim index As Integer = 0
        Dim indexfirst As Integer = 0
        Dim indexlast As Integer = 0

        indexlast = input.IndexOf(",")
        temp_testnumber = input.Substring(indexfirst, indexlast - indexfirst)
        TestNumber = stringtoint(temp_testnumber)

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        TestDate = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        TestTime = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        SerialInstrument = stringtoint(input.Substring(indexfirst + 1, indexlast - indexfirst - 1))

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        PatientID = stringtoint(input.Substring(indexfirst + 1, indexlast - indexfirst - 1))

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        PatientName = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

        indexfirst = input.IndexOf(",", indexlast + 2)
        indexlast = input.IndexOf(",", indexfirst + 1)
        OperatorID = stringtoint(input.Substring(indexfirst + 1, indexlast - indexfirst - 1))

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        TestProtocol = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        Color = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)


        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        Clarity = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

    End Sub

    'Hàm trả về kết quả một xét nghiệm với mã nhận dạng
    Public Function resulttest(ByVal input As String, ByVal identifier As String) As String
        Dim index As Integer = 0
        Dim indexfirst As Integer = 0
        Dim indexlast As Integer = 0
        Dim temp As String = ""
        index = input.IndexOf(identifier)
        indexfirst = input.IndexOf(",", index + 1)
        indexlast = input.IndexOf(",", indexfirst + 1)
        temp = input.Substring(indexfirst + 1, indexlast - indexfirst - 1)

        indexfirst = indexlast
        indexlast = input.IndexOf(",", indexfirst + 1)
        If (indexlast = -1) Then
            indexlast = input.IndexOf("*", indexfirst + 1)
            If (indexlast <> -1) Then
                indexlast = indexlast + 1
            Else
                indexlast = indexfirst + 1
            End If
        End If
        temp = temp + input.Substring(indexfirst + 1, indexlast - indexfirst - 1)
        Return temp
    End Function
    'Hàm trả về kết quả của tất cả các xét nghiệm
    Public Sub result(ByVal input As String, ByRef GLU As String, ByRef BIL As String, ByRef KET As String, ByRef SG As String, ByRef BLD As String, ByRef pH As String, ByRef PRO As String, ByRef UBG As String, ByRef NIT As String, ByRef LEU As String)
        input = cutstring(input)
        GLU = resulttest(input, "GLU")
        BIL = resulttest(input, "BIL")
        KET = resulttest(input, "KET")
        SG = resulttest(input, "SG")
        BLD = resulttest(input, "BLD")
        pH = resulttest(input, "pH")
        PRO = resulttest(input, "PRO")
        UBG = resulttest(input, "UBG")
        NIT = resulttest(input, "NIT")
        LEU = resulttest(input, "LEU")
    End Sub
    'Hàm chuyển một string thành một số integer
    Private Function stringtoint(ByVal input As String) As Integer
        Dim temp As Integer = 0
        Dim coso As Integer = 1
        Dim i As Integer
        For i = 0 To input.Length - 1
            temp = temp + chartoint(input((input.Length - i - 1))) * coso
            coso = coso * 10
        Next i

        Return temp
    End Function
    'hàm chuyển một char thành một số đếm
    Private Function chartoint(ByVal input As Char) As Integer
        Dim temp As Integer = 0

        Select Case input
            Case "0"c
                temp = 0
            Case "1"c
                temp = 1
            Case "2"c
                temp = 2
            Case "3"c
                temp = 3
            Case "4"c
                temp = 4
            Case "5"c
                temp = 5
            Case "6"c
                temp = 6
            Case "7"c
                temp = 7
            Case "8"c
                temp = 8
            Case "9"c
                temp = 9
        End Select
        Return temp
    End Function
    'Hàm cut bản tin gốc thành bản tin chỉ chứa các kết quả xét nghiệm
    Public Function cutstring(ByVal input As String) As String
        Dim temp As String = ""
        Dim index As Integer = 0
        Dim indexlast As Integer = 0
        Dim k As Integer = 0
        While 1
            index = input.IndexOf(",", indexlast + 1)
            If (index <> -1) Then
                indexlast = index
                k = k + 1
            End If
            If (k = 12) Then
                Exit While
            End If
        End While
        temp = input.Substring(indexlast + 1, input.Length() - indexlast - 1)
        Return temp
    End Function

    End Class
